<?php

class UsersController extends AdminAppController {
	
	public $uses = array("Admin.User");
	
	public $paginate = array (
		'limit' => 25,
		'order' => array('User.id' => 'desc')
	);
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->usersPicturePath = WWW_ROOT . "files" . DS . "profiles";
	}
	
	public function index() {
		$this->User->recursive = 0;
		$users = $this->paginate();
		$this->set(compact('users'));
	}
	
	public function view($id) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			$this->setUserInfo(__("Invalid User."), "error");
			$this->redirect( $this->referer() );
		}
		$this->set('user', $this->User->read(null, $id));
	}
	
	public function add() {
		if ($this->request->is('post')) {
			try {
				$file = $this->request->data['User']['picture'];
				unset($this->request->data['User']['picture']);
				
				// begin transaction
				$this->User->getDataSource()->begin();
				$this->User->create();
				if( $this->User->save( $this->request->data ) ) {
					if( !$file['error'] ) {
						$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
						$fileName = "profile_" . $this->User->id . "." . $ext;
						$this->request->data['User']['picture'] = $fileName;
						$this->Upload->allowed_types('image/jpeg', 'image/png', 'image/gif');
						$this->Upload->custom_name($fileName);
						$this->Upload->upload($file, $this->usersPicturePath);
						$this->User->set('picture', $fileName);
						$this->User->save();
					}
					$this->setUserInfo( __("User created."), "success" );
					// commit transaction
					$this->User->getDataSource()->commit();
					$this->redirect(array('action' => 'view', $this->User->id));
				}
				else {
					throw new Exception( __("A database error occured.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction
				$this->User->getDataSource()->rollback();
				// handle the exception
				$this->log("Error saving User: " . $ex->getMessage());
				$this->setUserInfo( __("The User could not be saved: ") . $ex->getMessage(), 'error');
			}
		}	
	}
	
	public function edit($id) {
		
		$this->User->id = $id;
		if (!$this->User->exists()) {
			$this->setUserInfo(__("Invalid User."), "error");
			$this->redirect( $this->referer() );
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			try {
				$file = $this->request->data['User']['picture'];
				unset($this->request->data['User']['picture']);
				
				// begin transaction
				$this->User->getDataSource()->begin();
				if( $this->User->save( $this->request->data ) ) {
					if( !$file['error'] ) {
						$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
						$fileName = "profile_" . $this->User->id . "." . $ext;
						$this->request->data['User']['picture'] = $fileName;
						$this->Upload->allowed_types('image/jpeg', 'image/png', 'image/gif');
						$this->Upload->custom_name($fileName);
						$this->Upload->upload($file, $this->usersPicturePath);
						$this->User->set('picture', $fileName);
						$this->User->save();
					}
					$this->setUserInfo( __("User updated."), "success" );
					// commit transaction
					$this->User->getDataSource()->commit();
					$this->redirect(array('action' => 'view', $this->User->id));
				}
				else {
					throw new Exception( __("A database error occured.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction
				$this->User->getDataSource()->rollback();
				// handle the exception
				$this->log("Error saving User: " . $ex->getMessage());
				$this->setUserInfo( __("The User could not be saved: ") . $ex->getMessage(), 'error');
			}
		}
		else {
			$this->request->data = $this->User->read(null, $id);
		}
	}
	
	public function delete($id) {
		
		$this->User->id = $id;
		if (!$this->User->exists()) {
			$this->setUserInfo(__("Invalid User."), "error");
			$this->redirect( $this->referer() );
		}
		
		$user = $this->User->read();
		
		if ($this->User->delete()) {
			// delete picture
			if( $user['User']['picture'] ) {
				$picturePath = $this->usersPicturePath . DS . $user['User']['picture'];
				if( @file_exists($picturePath) ) {
					@unlink($picturePath);
				}
			}
			$this->setUserInfo( __("User deleted."), 'success' );
			$this->redirect(array('action' => 'index'));
		}
		
		$this->setUserInfo( __("Couldn't delete the User."), 'error' );
		$this->redirect(array('action' => 'index'));
	}
	
}